﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ExtendedLayerRules.Models;
using Orchard;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Handlers;

namespace ExtendedLayerRules.Handlers
{
    public class CurrentContentHandler : ContentHandler
    {
        private readonly IWorkContextAccessor _workContextAccessor;

        public CurrentContentHandler(IWorkContextAccessor workContextAccessor)
        {
            _workContextAccessor = workContextAccessor;
        }

        protected override void BuildDisplayShape(BuildDisplayContext context)
        {
            var workContext = _workContextAccessor.GetContext();
            var contentItems = workContext.GetState<List<ContentTypeWithDisplayItem>>("ContentTypeWithDisplayItems");
            if (contentItems == null)
            {
                workContext.SetState("ContentTypeWithDisplayItems", contentItems = new List<ContentTypeWithDisplayItem>());
            }

            contentItems.Add(new ContentTypeWithDisplayItem(context.ContentItem.ContentType, context.DisplayType));
        }
    }
}